<?php

/**
 * @user magein
 * @date 2023/12/1 15:48
 */

namespace magein\think\utils\translate;

use magein\think\utils\constants\GenderCode;

trait SexTranslate
{
    /**
     * @param int|string|null $value
     * @return string|array
     */
    public static function translateSex($value = null)
    {
        $data = [
            GenderCode::SECRET => '未知',
            GenderCode::MAN => '男',
            GenderCode::WOMAN => '女'
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    /**
     * @param $value
     * @return int
     */
    public function setSexAttr($value): int
    {
        return intval($value);
    }

    /**
     * @param $value
     * @param $data
     * @return string
     */
    public function getSexTextAttr($value, $data): string
    {
        $value = $value ?: ($data['sex'] ?? '');
        if (empty($value)) {
            return '';
        }

        return self::translateSex($value);
    }
}